#include "TouchPadController.h"

#include "CommandParser.h"
#include "LoopManager.h"


TouchPadController::TouchPadController(DummyChannel& channel, const ExtendDeviceType& deviceType, const std::string& deviceActknowlege): ExtendDevice(channel, deviceType, deviceActknowlege)
    ,mpcommandParser(&CommandParserSingleton::instance()) {

}


bool TouchPadController::recheck() noexcept {

    bool recheckResult = true;
    std::string result;
    try {
        mchannel.sendStringReturnString(std::string("<GETSCENEID>"), result);
        if (result.empty()){
            recheckResult = false;
        } else if ("<GETSCENEID_ERROR>" == result) {
            LOG_ERROR(result.data());
        } else {
            char sceneId = result.at(12);

            LoopManager& loopManager = LoopManagerSingleton::instance();

            LoopSize_t loopSize = loopManager.updatePortCount();
            LoopControlInfoPtr ploopControlInfo(new(loopSize) LoopControlInfo{*loopManager.mpTimedLoopControlInfo}, LoopControlInfo::releaseMemory);
            ploopControlInfo->commandType = LoopCommandType::ONLINE_COMMAND;
            memset(ploopControlInfo->loopStatus, LoopStatus::AUTO_ON, sceneId - 48);
            loopManager.changeAutoLoopStatus(ploopControlInfo);

            std::string command(STRING_BUFFER_SIZE, 0);
            int written = ::snprintf(&command[0], command.size(), "<SHOWSCENEID_%d_%d>", sceneId - 48, 1);
            command.resize(written);
            mchannel.sendStringReturnString(command, result);
        }
//        Task* pTask = buildDynamicFunctor(&TouchPadController::updateDoorStatus, this, doorStatus);
//        mpool->pushPool(pTask);
    } catch (std::exception& ex) {
        LOG_ERROR(ex.what());
    }
    return recheckResult;
}










